package com.lemon.smartsafetynew.ui.adapter.ledger;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/4.
 */

public class LedgerBuildingListAdapter extends LemonRcvAdapter<LedgerBuildingItemVo> {

    private RecyclerTClickListener<LedgerBuildingItemVo> mListener;

    public LedgerBuildingListAdapter(@Nullable List<LedgerBuildingItemVo> data,
                                     RecyclerTClickListener<LedgerBuildingItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    private class ItemAdapter implements AdapterItem<LedgerBuildingItemVo> {

        private TextView monitorItemTv;
        private ImageView monitorStatusImageView;
        private TextView itemCountTv;
        private TextView itemNormalStatusTv;
        private TextView itemAbnormalStatusTv;
        private LinearLayout itemLayout;

        private LedgerBuildingItemVo mData;
        private int mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_ledger_building;
        }

        @Override
        public void bindViews(View root) {

            monitorItemTv = (TextView) root.findViewById(R.id.monitor_item_tv);
            monitorStatusImageView = (ImageView) root.findViewById(R.id.monitor_status_imageView);
            itemCountTv = (TextView) root.findViewById(R.id.item_count_tv);
            itemNormalStatusTv = (TextView) root.findViewById(R.id.item_normal_status_tv);
            itemAbnormalStatusTv = (TextView) root.findViewById(R.id.item_abnormal_status_tv);
            itemLayout = (LinearLayout) root.findViewById(R.id.item_layout);

        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(LedgerBuildingItemVo ledgerBuildingItemVo, int position) {

            mData = ledgerBuildingItemVo;
            mPosition = position;

            monitorItemTv.setText(CommonUtil.avoidNullString(ledgerBuildingItemVo.getBuilding_name()));
            itemCountTv.setText("点位数：" + CommonUtil.avoidNullString(ledgerBuildingItemVo.getItem_num()));

            if (ledgerBuildingItemVo.getRed_item_num() != null) {
                if ("0".equals(ledgerBuildingItemVo.getRed_item_num())) {
                    monitorStatusImageView.setVisibility(View.GONE);
                } else {
                    monitorStatusImageView.setVisibility(View.VISIBLE);
                }
            }

            itemNormalStatusTv.setText("正常点位：");
            if (ledgerBuildingItemVo.getGreen_item_num() != null
                    && !"0".equals(ledgerBuildingItemVo.getGreen_item_num())) {
                SpannableString normal = new SpannableString(ledgerBuildingItemVo.getGreen_item_num());
                normal.setSpan(new ForegroundColorSpan(Color.parseColor("#60cc86")), 0,
                        normal.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                itemNormalStatusTv.append(normal);
            } else {
                itemNormalStatusTv.append("0");
            }

            itemAbnormalStatusTv.setText("异常点位：");
            if (ledgerBuildingItemVo.getRed_item_num() != null
                    && !"0".equals(ledgerBuildingItemVo.getRed_item_num())) {
                SpannableString abNormal = new SpannableString(ledgerBuildingItemVo.getRed_item_num());
                abNormal.setSpan(new ForegroundColorSpan(Color.parseColor("#ea654f")), 0,
                        abNormal.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                itemAbnormalStatusTv.append(abNormal);
            } else {
                itemAbnormalStatusTv.append("0");
            }

        }
    }
}
